/*******************************************************************************
 * Copyright (c) 2000, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package samples;

/*
 * Browser example snippet: bring up a browser
 *
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 * 
 * @since 3.0
 */
import java.io.IOException;
import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.eclipse.swt.*;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.browser.*;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class BrowserWidget {
	public static void main(String [] args) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException {		
		String url = "http://gdata.youtube.com/feeds/api/videos?q=led+zeppelin+black+dog&max-results=10&v=2&format=5";
		
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(url);       
        NodeList list = doc.getElementsByTagName("entry");
        
        XPath xpath = XPathFactory.newInstance().newXPath();
        NodeList entries = (NodeList) xpath.evaluate("//entry", doc, XPathConstants.NODESET);
		
		final String html = "<html style='margin: 0px;'><body><object width='480' height='295'><param name='movie' value='%s'></param><param name='allowFullScreen' value='false'></param><param name='allowscriptaccess' value='always'></param><embed src='%s' type='application/x-shockwave-flash' width='480' height='295' allowscriptaccess='always' allowfullscreen='true'></embed></object></body></html>";

		Display display = new Display();
		Shell shell = new Shell(display);
		shell.setLayout(new RowLayout(SWT.VERTICAL));
		
		try {
			final Browser browser = new Browser(shell, SWT.NONE);
			browser.setLayoutData(new RowData(500, 300));
			
			// Create links
			for (int i = 0; i < entries.getLength(); i++) {
				Node entry = entries.item(i);
				Link link = new Link(shell, SWT.FILL);
				link.setText("<a>" + xpath.evaluate("title/text()", entry) + "</a>");
				link.setData(xpath.evaluate("content/@src", entry));
				link.addListener (SWT.Selection, new Listener () {
					public void handleEvent(Event event) {
						String movieUrl = event.widget.getData().toString();
						browser.setText(String.format(html, movieUrl, movieUrl));
					}
				});
			}					
			
		} catch (SWTError e) {
			System.out.println("Could not instantiate Browser: " + e.getMessage());
			display.dispose();
			return;
		}		
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}
}